limsFilemanagement.detailPage = {
    getBtnToolbar: function (id, step) {
        var _this = this
        var buttons = [{
            type: 'button',
            text: 'GIKAM.BUTTON.BACK',
            icon: 'back',
            onClick: function () {
                workspace.window.goBack()
            }
        },]
        if (step) {
            if (step === 'edit') {
            } else {

            }
        }
        return {
            type: 'btnToolbar',
            items: buttons
        }
    },

    getFileVersionGrid: function (gridId, id, step) {
        var _this = this
        var toolbar = []
        var requestData = {
            fileId_EQ: id
        }
        var columns = limsFilemanagementVersion.getEditListColumns()
        if (step) {
            if (step === 'edit') {
                toolbar.push({
                    type: 'button',
                    text: 'GIKAM.BUTTON.INSERT',
                    icon: 'add',
                    onClick: function () {
                        if (Gikam.isEmpty(id)) {
                            Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM')
                            return
                        }
                        Gikam.preInsert({
                            modalTitle: 'BASEMODULE.RM.FILEMANAGEMENTS.MODAL.ADD',
                            fields: limsFilemanagementVersion.getBaseInfoFormFields(),
                            url: limsFilemanagementVersion.baseUrl,
                            param: {
                                fileId: id
                            },
                            titleWidth: 100,
                        }).done(function () {
                            Gikam.getComp(gridId).refresh()
                        })
                    }
                }, {
                    type: 'button',
                    text: 'GIKAM.BUTTON.DELETE',
                    icon: 'remove-row',
                    onClick: function () {
                        Gikam.getComp(gridId).deleteRows(limsFilemanagementVersion.baseUrl)
                    }
                })
            } else if (step === 'search') {
                requestData.versonActiveFilter = '1'
                columns = limsFilemanagementVersion.getSearchListColumns()
            }
        }
        return {
            type: 'grid',
            id: gridId,
            service: 'limsFilemanagementVersionServiceImpl',
            dbTable: 'T_LIMS_FILEMANAGEMENT_VERSION',
            columns: columns,
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                } else {
                    Gikam.getAsyncComp(gridId + '-uploader').done(uploader => {
                        uploader.setOptions({
                            bizId: -1,
                        })
                    })
                }
            },
            deleteFormatter: function (row) {
                return row.versionCode || row.id
            },
            onRowActive: function (index, row) {
                Gikam.getAsyncComp(gridId + '-uploader').done(uploader => {
                    uploader.setOptions({
                        bizId: row.id,
                    })
                })
            },
            url: limsFilemanagementVersion.baseUrl + '/queries',
            requestData: requestData,
            toolbar: toolbar
        }
    },

    getAttachmantStruct: function (gridId, dbTable, step) {
        return {
            type: 'uploader',
            immediateUpload: true,
            id: gridId + '-uploader',
            dbTable: dbTable,
            // bizId: -1,
            filter: false,
            genericQuery: false,
            onBeforeUpload: function () {
                var activedRow = Gikam.getComp(gridId).getActivedRow()
                if (Gikam.isEmpty(activedRow)) {
                    Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200)
                    return false
                }
            },
            readonly: !step || (step !== 'edit')
        }
    },

    create: function () {
        var _this = this
        Gikam.create('layout', {
            renderTo: workspace.window.$dom,
            north: {
                height: '55%',
                items: [this.getBtnToolbar(_this.param.id, _this.param.step), _this.getFileVersionGrid(_this.idManager(_this.param.step).fileVersionId, _this.param.id, _this.param.step)],
            },
            center: {
                items: [_this.getAttachmantStruct(_this.idManager(_this.param.step).fileVersionId, 'T_LIMS_FILEMANAGEMENT_VERSION', _this.param.step)]
            }
        })
    },

    idManager: function (step) {
        return {
            fileGridId: 'lims-filemanagement-edit-list-grid' + step,
            fileVersionId: 'lims-filemanagement-version-edit-list-grid' + step,
        }
    },

    init: function (param) {
        this.param = param
        this.create()
    }
}
